<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="dynamicFormRules">
                <el-form-item label="主键ID" v-show="isEditShow" prop="id">
                    <el-input v-model="formData.id" disabled placeholder="请输入主键ID" />
                </el-form-item>
                <el-form-item label="配置标题" prop="title">
                    <el-input v-model="formData.title" placeholder="请输入配置标题" />
                </el-form-item>
                <el-form-item label="使用标识" prop="flag">
                    <el-input v-model="formData.flag" placeholder="请输入使用标识" />
                </el-form-item>
                <el-form-item label="商户编号" prop="mer_no">
                    <el-input v-model="formData.mer_no" placeholder="请输入商户编号" />
                </el-form-item>
                <el-form-item label="商户账号" prop="mer_account">
                    <el-input v-model="formData.mer_account" placeholder="请输入商户账号" />
                </el-form-item>
                <el-form-item label="私钥" prop="private_key">
                    <el-input v-model="formData.private_key" placeholder="请输入私钥" />
                </el-form-item>
                <el-form-item label="公钥" prop="public_key">
                    <el-input v-model="formData.public_key" placeholder="请输入公钥" />
                </el-form-item>

                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="formData.status" placeholder="请选择状态">
                        <el-radio v-for="item in payStatus.dictData.pay_status" :label="Number(item.value)" :value="Number(item.value)" :key="Number(item.value)"> {{ item.name }}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  configsEdit, configsAdd, configsDetail } from '@/api/client/pay_configs'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import { useDictData } from '@/hooks/useDictOptions'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑支付配置' : '新增支付配置'
})
const isEditShow = computed(() => mode.value === 'edit')

const formData = reactive({
    id: '',
    title: '',
    flag: '',
    mer_no: '',
    mer_account: '',
    private_key: '',
    public_key: '',
    status: 1,
    package_name: '',
})


interface DictItem {
    value: string | number
    name: string
}

interface PayStatusDictData {
    pay_status: DictItem[]
}

const payStatus = useDictData<PayStatusDictData>(['pay_status'])

const formRules = {
    id: [
        {
            required: true,
            message: '请输入主键ID',
            trigger: ['blur']
        }
    ],
    title: [
        {
            required: true,
            message: '请输入配置标题',
            trigger: ['blur']
        }
    ],
    flag: [
        {
            required: true,
            message: '请输入使用标识',
            trigger: ['blur']
        }
    ],
    mer_no: [
        {
            required: true,
            message: '请输入商户编号',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择状态',
            trigger: ['blur']
        }
    ],
    package_name: [
        {
            required: true,
            message: '请输入应用标识',
            trigger: ['blur']
        }
    ],
}

// 修改为动态规则
const dynamicFormRules = computed(() => {
    const rules = { ...formRules }

    // 在添加模式下，account_id 不需要必填
    if (mode.value === 'add') {
        rules.id = [
            {
                required: false,
                message: '请输入账号标识',
                trigger: ['blur']
            }
        ]
    }

    return rules
})


const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await configsEdit(data) : await configsAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await configsDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
